1 00:00:00,710 --> 00:00:01,760 Welcome back. 2 00:00:01,760 --> 00:00:06,800 In this lecture we're going to take a look at a core principle of object oriented programming called 3 00:00:06,800 --> 00:00:07,880 inheritance. 4 00:00:07,910 --> 00:00:14,120 Now inheritance is basically just how a class can inherit properties and behaviors from another class. 5 00:00:14,120 --> 00:00:19,100 The class that is inheriting properties from another class is known as a subclass, while the class 6 00:00:19,100 --> 00:00:23,690 that is providing the properties and behaviors to inherit is called the superclass. 7 00:00:24,200 --> 00:00:26,240 So let's do a quick example of inheritance. 8 00:00:26,240 --> 00:00:31,130 Using tables and meta tables, I'm going to create a new table called animal. 9 00:00:32,970 --> 00:00:36,030 And this table is going to act as my superclass. 10 00:00:36,060 --> 00:00:39,390 Now what are some properties that all animals will have? 11 00:00:39,390 --> 00:00:43,140 Well, let's say all animals will have a name, right? 12 00:00:43,140 --> 00:00:46,770 We don't know the current name of the animal, so we'll set it to unknown. 13 00:00:47,300 --> 00:00:51,350 And maybe another property we want all animals to have is a sound they make. 14 00:00:51,350 --> 00:00:55,430 So maybe a dog, you know, barks, a cat, meows, things like that. 15 00:00:55,430 --> 00:01:00,500 So we'll just set the silent because again, this is just representing our blueprint for creating an 16 00:01:00,500 --> 00:01:01,160 animal. 17 00:01:01,910 --> 00:01:05,120 And again, this is going to be our superclass. 18 00:01:05,690 --> 00:01:11,390 Now we're going to have to make sure to set the meta method of underscore underscore index equal to 19 00:01:11,390 --> 00:01:12,620 the animal table. 20 00:01:12,620 --> 00:01:17,150 That's because we're going to be using this table as our meta table for another class. 21 00:01:17,150 --> 00:01:21,500 And for it to be able to access these properties, we need to make sure to set the index meta method 22 00:01:21,500 --> 00:01:22,850 to the table itself. 23 00:01:23,150 --> 00:01:26,300 Now what's some functions that all animals will have? 24 00:01:26,420 --> 00:01:33,050 Well, maybe a function that all animals could have is the ability to speak I guess, or emit their 25 00:01:33,050 --> 00:01:33,470 sound. 26 00:01:33,470 --> 00:01:35,480 So we'll call this function speak. 27 00:01:35,480 --> 00:01:39,830 And inside this function we can just print um self dot name. 28 00:01:39,830 --> 00:01:46,400 The name of our animal says and then we can do self dot sound. 29 00:01:46,430 --> 00:01:48,230 So that's what our function will do. 30 00:01:48,230 --> 00:01:51,920 And now we have a basic outline for our superclass. 31 00:01:51,920 --> 00:01:54,320 So let's go ahead and create another class. 32 00:01:54,320 --> 00:01:56,420 And this class is going to be our subclass. 33 00:01:56,420 --> 00:01:59,330 So let's make a class for a cat. 34 00:02:00,110 --> 00:02:02,510 So this is going to be our subclass. 35 00:02:03,280 --> 00:02:08,560 Make sure to set the underscore underscore index meta method to the cat class itself. 36 00:02:08,560 --> 00:02:11,920 Again, because we're going to be using this as a meta table. 37 00:02:12,220 --> 00:02:18,130 And then in order for this class to inherit all of these properties from the animal class, we need 38 00:02:18,130 --> 00:02:24,130 to actually set the meta table for this cat class to the animal class. 39 00:02:24,160 --> 00:02:29,500 Now, our cat class will be able to inherit the properties like the name, the sound, and have access 40 00:02:29,500 --> 00:02:31,120 to this function like speak. 41 00:02:31,120 --> 00:02:35,830 So that means we can go down here and set cat dot name equal to cat. 42 00:02:36,850 --> 00:02:41,140 We can do cat dot sound equal to something like meow. 43 00:02:42,480 --> 00:02:46,800 And because it's going to be our subclass, there's obviously going to be some things that cats can 44 00:02:46,800 --> 00:02:48,570 do that other animals can't do. 45 00:02:48,570 --> 00:02:52,860 So maybe, for example, only a cat can sneak around. 46 00:02:52,860 --> 00:02:56,610 And inside this function we can just print self dot name. 47 00:02:57,520 --> 00:02:59,500 Is sneaking around. 48 00:03:00,850 --> 00:03:07,420 So this function here belongs to our cat class, while this function here belongs to our superclass, 49 00:03:07,420 --> 00:03:08,740 the animal class. 50 00:03:09,720 --> 00:03:12,330 And that means we can go ahead and create a new cat object. 51 00:03:12,330 --> 00:03:13,890 I'll call it my cat. 52 00:03:14,400 --> 00:03:17,520 And what we'll do is we'll call the set Metatable function. 53 00:03:17,520 --> 00:03:19,020 Create a new table. 54 00:03:19,730 --> 00:03:23,930 And set the meta table for this table to the cat class. 55 00:03:23,930 --> 00:03:26,990 So this is going to be our new cat object. 56 00:03:26,990 --> 00:03:32,630 And because we've created this new cat object that means we can call functions like speak on it. 57 00:03:33,880 --> 00:03:38,770 Now, what happens when this function gets called is that the interpreter is going to check to see whether 58 00:03:38,770 --> 00:03:42,280 or not this table here has the speak function in it. 59 00:03:42,280 --> 00:03:47,350 When it checks, it's going to see, hey, this index doesn't exist within the my cat table. 60 00:03:47,350 --> 00:03:51,550 So let's go ahead and check to see if the my cat table has a meta table, which it does. 61 00:03:51,550 --> 00:03:53,680 And that's going to be the cat table. 62 00:03:53,680 --> 00:03:58,210 So it's going to go into the cat table and check to see if it has an underscore underscore index meta 63 00:03:58,210 --> 00:03:59,590 method which it does. 64 00:03:59,590 --> 00:04:05,440 And it's going to check that table that's attached to this meta method to see if it has the speak function. 65 00:04:05,440 --> 00:04:09,790 Unfortunately our cat class doesn't have a speak function. 66 00:04:09,790 --> 00:04:16,000 So it's going to check if the cat table has a meta table with an index meta method, which it does because 67 00:04:16,000 --> 00:04:17,200 we set it right here. 68 00:04:17,200 --> 00:04:24,550 So it's going to go ahead and look inside of the animal class and it'll see hey it has an index for 69 00:04:24,550 --> 00:04:25,150 this table. 70 00:04:25,150 --> 00:04:30,670 It's going to check this table and it'll see hey this table has the speak function. 71 00:04:30,670 --> 00:04:37,570 So let's use this function and it'll pass the original table that we called the speak function on to 72 00:04:37,720 --> 00:04:40,210 the function right here as self. 73 00:04:40,210 --> 00:04:44,470 So that means this function should print cat says meow. 74 00:04:45,160 --> 00:04:50,050 And then we can also call the sneak function on our cat table as well. 75 00:04:50,620 --> 00:04:54,670 So let's go ahead and run the game and let's see what gets printed in the console. 76 00:04:56,560 --> 00:04:57,160 There we go. 77 00:04:57,160 --> 00:05:01,300 We get Cat says meow and Cat is sneaking around. 78 00:05:03,000 --> 00:05:08,130 So this is a very basic example of how you can use inheritance in Roblox Lua. 79 00:05:08,190 --> 00:05:09,840 It's actually pretty simple. 80 00:05:09,840 --> 00:05:16,140 All we need to do is set the meta table for any classes to inherit from another class. 81 00:05:16,140 --> 00:05:21,840 Again, the class that will be inheriting stuff from another class is called the subclass, while the 82 00:05:21,840 --> 00:05:26,190 class that is giving those inheritable items is called the superclass. 83 00:05:26,760 --> 00:05:29,520 So I hope you learned a lot and I'll see you in the next lecture.